package reggie.ruiji.service.impl;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reggie.ruiji.Dto.DishDto;
import reggie.ruiji.Dto.SetmealDto;
import reggie.ruiji.except.Customexception;
import reggie.ruiji.mapper.CategoryMapper;
import reggie.ruiji.mapper.SetmealMapper;
import reggie.ruiji.mapper.SetmealdishMapper;
import reggie.ruiji.pojo.*;
import reggie.ruiji.service.SetmealService;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService
{
   @Autowired
   private SetmealMapper setmealMapper;

   @Autowired
   private SetmealdishMapper setmealdishMapper;

   @Autowired
   private CategoryMapper categoryMapper;




    @Override
    public Long count(Long id)
    {
        return setmealMapper.countByid(id);
    }

    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto, HttpServletRequest request)
    {
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setStatus(1);
        setmealDto.setCode("1");
        Long empID=(Long)request.getSession().getAttribute("employee");
        setmealDto.setUpdateUser(empID);
        setmealDto.setCreateUser(empID);
        setmealMapper.insert(setmealDto);
        Long setmealID=setmealMapper.getIdByName(setmealDto.getName());
        List<SetmealDish> list=setmealDto.getSetmealDishes();
        for(SetmealDish setmealDish:list)
        {
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(empID);
            setmealDish.setCreateUser(empID);
            setmealDish.setSetmealId(setmealID);
        }
        setmealdishMapper.insert(list);

    }

    @Override
    public PageBean pagee(Integer pageNo, Integer pageSize, String name) {
        List<SetmealDto> emplist = setmealMapper.list(name);
        for(SetmealDto setmealDto:emplist)
        {
            String  categoryName=categoryMapper.getNameById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(categoryName);
        }
        PageBean pageBean = new PageBean(emplist, emplist.size(), pageSize, pageNo);
        return pageBean;

    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        setmealMapper.updateStatus(status,ids);

    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {

        int count=setmealMapper.getnumberByStatus(ids);
        if(count>0)
        {
            throw new Customexception("套餐正在售卖中，不能删除");
        }
        setmealMapper.delete(ids);
        setmealdishMapper.delete(ids);

    }

    @Override
    public SetmealDto getById(Long id) {
        SetmealDto setmealDto=new SetmealDto();
        Setmeal setmeal= setmealMapper.getById(id);
        String name=categoryMapper.getNameById(setmeal.getCategoryId());
        List<SetmealDish> list=setmealdishMapper.getBySetmealId(id);
        setmealDto.setSetmealDishes(list);
        setmealDto.setCategoryName(name);
        BeanUtils.copyProperties(setmeal,setmealDto);
        return setmealDto;
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        Integer  status = 1;//只查询起售状态下的菜品

        List<Setmeal> list =setmealMapper.getSetmealByCategoryId(setmeal.getCategoryId(),status);
        return list;
    }


}
